﻿using System;
using System.Collections.Generic;
using System.Text;
using MediaPortal.GUI.Library;
using MediaPortal.Player;
using MediaPortal.Dialogs;
using MediaPortal.Util;
using MediaPortal.Playlists;
using MediaPortal.Video.Database;
using MediaPortal.Video;

using System.Windows.Forms;


namespace BierdopjeTV
{
    class VideoHandler
    {
        static PlayListPlayer playlistPlayer;

        public bool isPlaying = false;
        public int EpisodeID = 0;

        public VideoHandler()
        {
            playlistPlayer = PlayListPlayer.SingletonPlayer;

            g_Player.PlayBackStopped += new MediaPortal.Player.g_Player.StoppedHandler(OnPlayBackStopped);
            g_Player.PlayBackEnded += new MediaPortal.Player.g_Player.EndedHandler(OnPlayBackEnded);
            g_Player.PlayBackStarted += new MediaPortal.Player.g_Player.StartedHandler(OnPlayBackStarted);
        }

        

        public void Play(string File)
        {
            Play(File, -1, null);
        }

        private void Play(string File, int timeMovieStopped, byte[] resumeData)
        {
            Helpers.Logger.Write("Proberen af te spelen...", "Play()");
            try
            {
                playlistPlayer.Reset();
                playlistPlayer.CurrentPlaylistType = PlayListType.PLAYLIST_VIDEO_TEMP;
                PlayList playlist = playlistPlayer.GetPlaylist(PlayListType.PLAYLIST_VIDEO_TEMP);
                playlist.Clear();

                PlayListItem itemNew = new PlayListItem();
                itemNew.FileName = File;
                itemNew.Type = PlayListItem.PlayListItemType.Video;
                playlist.Add(itemNew);
                playlistPlayer.Play(0);
     
            }
            catch (Exception e)
            {
                Helpers.Logger.Write("ERROR: " + e.Message, "Play()");
            }
        }

  

        private void OnPlayBackStopped(MediaPortal.Player.g_Player.MediaType type, int timeMovieStopped, string filename)
        {
            Helpers.Logger.Write("Playback Stopped", "OnPlayBackStopped()");
            MarkAsWachted(timeMovieStopped, playlistPlayer.g_Player.Duration);         
        }

        private void OnPlayBackEnded(MediaPortal.Player.g_Player.MediaType type, string filename)
        {
            Helpers.Logger.Write("Playback ended", "OnPlayBackEnded()");
            MarkAsWachted(Convert.ToInt32(playlistPlayer.g_Player.CurrentPosition), playlistPlayer.g_Player.Duration);
        }

        private void OnPlayBackStarted(MediaPortal.Player.g_Player.MediaType type, string filename)
        {
            Helpers.Logger.Write("Playback started.", "OnPlayBackStarted()");            
        }

        private void MarkAsWachted(int StopTijd, double Duratie)
        {            
            int db_percentage = Convert.ToInt32(Database.Database.getSettingValue("EpisodeGezien"));

            double Percentage = (double)db_percentage / 100;

            if (StopTijd >= (Duratie * Percentage))
            {
                Helpers.Logger.Write("Aflevering " + EpisodeID.ToString() + " is nu gezien.", "MarkAsWachted()");
                System.Diagnostics.Debug.Print("90% of meer gegekeken");
                Database.Database.MarkAsWatched(EpisodeID);
            }
            else
            {
                Helpers.Logger.Write("Aflevering " + EpisodeID.ToString() + " heeft niet de status bekeken.", "MarkAsWachted()");
                System.Diagnostics.Debug.Print("89% of minder gegekeken");
            }
        }

    }
}